/**
 * @file docs.js
 * @author Alejandro Dario Simi
 * @date $Date: 2013-08-05 00:26:28 +0000 (Mon, 05 Aug 2013) $
 *
 * $Id: docs.js 100 2013-08-05 00:26:28Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/docs/docs.js $
 */

function AddFooter() {
	$.get("footer.html", function(data){
		$("body").append(data);
	});
}

function CreateTableOfContents() {
	$("body").html($("body").html()
		.replace(/__TOC__/g,'<div class="Section"><span class="Title">Table Of Contents</span><div class="TableOfContents"></div></div>')
	);

	$(".TableOfContents").each(function(){
		var level = 1;
		var list  = "<ul>";

		$("div.Section span.Title,div.Section span.SubTitle").each(function() {
			if($(this).attr('class') == "SubTitle") {
				switch(level) {
					case 1:
						level = 2;
						list += "<ul>";
				}
			}
			if($(this).attr('class') == "Title") {
				switch(level) {
					case 2:
						level = 1;
						list += "</ul>";
				}
			}
			list += "<li><a href=\"#" + $(this).text() + "\">" + $(this).text() + "</a></li>";
			$(this).html("<hr class=\"" + $(this).attr('class') + "\"/><a name=\"" + $(this).text() + "\">" + $(this).text() + "</a> <sup><a href=\"#\">[back]</a></sup>");
		});

		list+= "</ul>";
		$(this).html(list);
	});
}

function ReplacePseudoCode() {
	$("body").html($("body").html()
		.replace(/\[VA:([^\]]*)\]/g,'<span class="Variable">$1</span>')
		.replace(/\[CL:([^\]]*)\]/g,'<span class="Class">$1</span>')
		.replace(/\[CO:([^\]]*)\]/g,'<span class="Constant">$1</span>')
		.replace(/\[ME:([^\]]*)\]/g,'<span class="Method">$1</span>')
		.replace(/\[TA:([^\]]*)\]/g,'<span class="Table">$1</span>')
		.replace(/\[AB:([^\]]*)\]/g,'<span class="Abstract">$1</span>')
		.replace(/\[OL:([^\]]*)\]/g,'<span class="Optional">$1</span>')

		.replace(/\{VA:([^\}]*)\}/g,'<span class="Variable">$1</span>')
		.replace(/\{CL:([^\}]*)\}/g,'<span class="Class">$1</span>')
		.replace(/\{CO:([^\}]*)\}/g,'<span class="Constant">$1</span>')
		.replace(/\{ME:([^\}]*)\}/g,'<span class="Method">$1</span>')
		.replace(/\{TA:([^\}]*)\}/g,'<span class="Table">$1</span>')
		.replace(/\{AB:([^\}]*)\}/g,'<span class="Abstract">$1</span>')
		.replace(/\{OL:([^\}]*)\}/g,'<span class="Optional">$1</span>')

		.replace(/\[L:([^\]]*)::([^\]]*)\]/g,'<a href="$1">$2</a>')
		.replace(/\[L\|([^\]]*)\|\|([^\]]*)\]/g,'<a href="$1">$2</a>')
		
		.replace(/\[wcomix\]/g,'<span class="WComix">wcomix</span>')

		.replace(/@todo/gi,'<span class="TODO">@TODO</span>')
	);
}

// Adding description for abstract method labels
function AbstractLabels() {
	$("div.Section div.Paragraph span.Abstract").each(function(){
		$(this).html($(this).html() + "<sup>abstract</sup>");
		$(this).attr("title", "Subclass Resposabillity");
	});
}

// Adding description for optional labels
function OptionalLabels() {
	$("div.Section div.Paragraph span.Optional").each(function(){
		$(this).html($(this).html() + "<sup>optional</sup>");
	});
}

function AddPageTitle() {
	$("body").prepend('<span class="PageTitle">' + $("head title").text() + '</span>');
	if($("#wc_index").length == 0) {
		$("body").prepend('<a class="PageTitleBack" href=\"index.html\">[back to index]</a>');
	}
	$("head").append('<link rel="icon" type="image/png" href="images/wcomix.png"/>');
}

function AddSyntaxHighlighter() {
	if($("pre[class^='brush: wcparser;']").length) {
		$("head").append('<script type="text/javascript" src="scripts/shBrushWCParser.js"></script>');
	}
	if($("pre[class^='brush: dtd;']").length) {
		$("head").append('<script type="text/javascript" src="scripts/shBrushDTD.js"></script>');
	}
	if($("pre[class^='brush: js;']").length) {
		$("head").append('<script type="text/javascript" src="SyntaxHighlighter/scripts/shBrushJScript.js"></script>');
	}
	if($("pre[class^='brush: php;']").length) {
		$("head").append('<script type="text/javascript" src="SyntaxHighlighter/scripts/shBrushPhp.js"></script>');
	}
	if($("pre[class^='brush: xml;']").length) {
		$("head").append('<script type="text/javascript" src="SyntaxHighlighter/scripts/shBrushXml.js"></script>');
	}
	if($("pre[class^='brush: sql;']").length) {
		$("head").append('<script type="text/javascript" src="SyntaxHighlighter/scripts/shBrushSql.js"></script>');
	}
}

function ParsePage() {
	AddSyntaxHighlighter();
	
	CreateTableOfContents();
	AbstractLabels();
	OptionalLabels();
	AddPageTitle();
	ReplacePseudoCode();

	AddFooter();
}
 
$(function(){
	ParsePage();
});
